package org.jeecg.modules.relation.relclssubtearoom.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.common.api.ISysInfo;
import org.jeecg.modules.common.baseentity.BaseApiEntity;
import org.jeecg.modules.common.utils.StringUtil;
import org.jeecg.modules.relation.relclassstudent.vo.RelClassesAndStudentsVO;
import org.jeecg.modules.relation.relclssubtearoom.dto.RelClassSubjectTeacherListParam;
import org.jeecg.modules.relation.relclssubtearoom.entity.RelClassSubjectTeacher;
import org.jeecg.modules.relation.relclssubtearoom.service.IRelClassSubjectTeacherService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.relation.relclssubtearoom.vo.RelClassSubjectTeacherVO;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: rel_class_subject_teacher
 * @author: baochangrong
 * @Date: 2022-02-16
 * @Version: V1.0
 */
@Api(tags = "rel_class_subject_teacher")
@RestController
@RequestMapping("/rel_class_subject_teacher/relClassSubjectTeacher")
@Slf4j
public class RelClassSubjectTeacherController extends JeecgController<RelClassSubjectTeacher, IRelClassSubjectTeacherService> {
    @Autowired
    private IRelClassSubjectTeacherService relClassSubjectTeacherService;
    @Autowired
    private ISysInfo sysInfo;
    /**
     * 分页列表查询
     *
     * @param relClassSubjectTeacher
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "rel_class_subject_teacher-分页列表查询")
    @ApiOperation(value = "rel_class_subject_teacher-分页列表查询", notes = "rel_class_subject_teacher-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(RelClassSubjectTeacher relClassSubjectTeacher,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        relClassSubjectTeacher.setCreateTime(null);
        QueryWrapper<RelClassSubjectTeacher> queryWrapper = QueryGenerator.initQueryWrapper(relClassSubjectTeacher, req.getParameterMap());
        Page<RelClassSubjectTeacher> page = new Page<RelClassSubjectTeacher>(pageNo, pageSize);
        IPage<RelClassSubjectTeacher> pageList = relClassSubjectTeacherService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    /**
     * 行转列 动态数据查询
     *
     * @param relClassSubjectTeacher
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "rel_class_subject_teacher-分页列表查询")
    @ApiOperation(value = "rel_class_subject_teacher-分页列表查询", notes = "rel_class_subject_teacher-分页列表查询")
    @GetMapping(value = "/listPro")
    public Result<?> queryPageListPro(RelClassSubjectTeacher relClassSubjectTeacher,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      HttpServletRequest req) throws NoSuchFieldException, IllegalAccessException {
        //组装vo以外的其他参数
        Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
        //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo
        BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,relClassSubjectTeacher,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

//        relClassSubjectTeacherVO.setCreateTime(null);
        Page<RelClassSubjectTeacherVO> page = new Page<RelClassSubjectTeacherVO>(pageNo, pageSize);
        List<JSONObject> pageList = relClassSubjectTeacherService.queryPageListPro(page, relClassSubjectTeacher,queryParam);
        return Result.OK(pageList);
    }


    @AutoLog(value = "rel_class_subject_teacher-设置各个班级任课老师")
    @ApiOperation(value = "rel_class_subject_teacher-设置各个班级任课老师", notes = "rel_class_subject_teacher-设置各个班级任课老师")
    @PutMapping(value = "/saveupdateClassSubjectTeacherRel")
    public Result<?> saveupdateClassSubjectTeacherRel(@RequestBody RelClassSubjectTeacherListParam relClassSubjectTeacherList,HttpServletRequest req){
        //组装vo以外的其他参数
        Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
        //从请求中获取公共参数信息：用户、部门、租户、学期、学年等公共信息。   //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo
        BaseApiEntity baseApiEntity = sysInfo.getBaseApiEntity(req);//.querySysInfoCloneObj(req,relClassSubjectTeacherList,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

        Integer saveCount = relClassSubjectTeacherService.saveupdateClassSubjectTeacherRel(relClassSubjectTeacherList,baseApiEntity);
        return Result.OK("更新成功!["+saveCount+"]条记录已更新");
    }

    @GetMapping(value = "/queryTeacherClassListBySubject")
    public Result<?> queryTeacherClassListBySubject(RelClassSubjectTeacherVO relClassSubjectTeacherVO,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                     HttpServletRequest req) {

        //组装vo以外的其他参数
        Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
        //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo
        BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,relClassSubjectTeacherVO,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo

        //根据用户身份重新设置查询条件值，不同的用户身份查询过滤不同的数据：
        //1.获取用户身份 userIdentity loginUser.getUserIdentity() 用户身份字典码user_identity， 3教师 4学生 5家长 6班级  1普通用户 2上级部门负责人
        //2.获取用户id loginUser.getId()
        //userIdentity 用户身份字典码user_identity， 3教师 4学生 5家长 6班级  1普通用户 2上级部门负责人
        //例如：设置普通老师只能查看自己的数据的时候，需要实现下面的方法
//        relClassSubjectTeacherVO = reSetObjectValue(baseApiEntity, relClassSubjectTeacherVO);

//        Page<RelClassSubjectTeacherVO> page = new Page<>(pageNo, pageSize);
//        IPage<RelClassSubjectTeacherVO> pageList = relClassSubjectTeacherService.queryTeacherClassListBySubject(relClassSubjectTeacherVO, queryParam);
//        return Result.OK(pageList);
        //暂时不考虑分页
        return Result.OK(relClassSubjectTeacherService.queryTeacherClassListBySubject(relClassSubjectTeacherVO, queryParam));

    }
    /**
     * 动态查询页面显示的列：科目列，动态查询显示
     *
     * @param relClassSubjectTeacherVO
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "rel_class_subject_teacher-动态显示列")
    @ApiOperation(value = "rel_class_subject_teacher-动态显示列", notes = "rel_class_subject_teacher-动态显示列")
    @GetMapping(value = "/getColumnsList")
    public Result<?> getColumnsList(RelClassSubjectTeacherVO  relClassSubjectTeacherVO,
                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                    HttpServletRequest req){
        //组装vo以外的其他参数
        Map<String,String> queryParam = sysInfo.getMapQueryParamByReq(req);
        //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo
        BaseApiEntity baseApiEntity = sysInfo.querySysInfoCloneObj(req,relClassSubjectTeacherVO,false); //获取当前登录用户信息 把租户 部门 身份 学期 学年等基本信息信息赋值给vo
        //暂时不考虑分页
        return Result.OK(relClassSubjectTeacherService.getColumnsList(relClassSubjectTeacherVO, queryParam));

    }

    /**
     * 添加
     *
     * @param relClassSubjectTeacher
     * @return
     */
    @AutoLog(value = "rel_class_subject_teacher-添加")
    @ApiOperation(value = "rel_class_subject_teacher-添加", notes = "rel_class_subject_teacher-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody RelClassSubjectTeacher relClassSubjectTeacher) {
        relClassSubjectTeacherService.save(relClassSubjectTeacher);
        return Result.OK("添加成功！");
    }


    /**
     * 编辑
     *
     * @param relClassSubjectTeacher
     * @return
     */
    @AutoLog(value = "rel_class_subject_teacher-编辑")
    @ApiOperation(value = "rel_class_subject_teacher-编辑", notes = "rel_class_subject_teacher-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody RelClassSubjectTeacher relClassSubjectTeacher) {
        relClassSubjectTeacherService.updateById(relClassSubjectTeacher);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "rel_class_subject_teacher-通过id删除")
    @ApiOperation(value = "rel_class_subject_teacher-通过id删除", notes = "rel_class_subject_teacher-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        relClassSubjectTeacherService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "rel_class_subject_teacher-批量删除")
    @ApiOperation(value = "rel_class_subject_teacher-批量删除", notes = "rel_class_subject_teacher-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.relClassSubjectTeacherService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "rel_class_subject_teacher-通过id查询")
    @ApiOperation(value = "rel_class_subject_teacher-通过id查询", notes = "rel_class_subject_teacher-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        RelClassSubjectTeacher relClassSubjectTeacher = relClassSubjectTeacherService.getById(id);
        if (relClassSubjectTeacher == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(relClassSubjectTeacher);
    }

    /**
     * 升级
     *
     * @param
     * @return
     */
    @AutoLog(value = "insertUpgradeRelClassSubjectTeacher-升级")
    @ApiOperation(value = "insertUpgradeRelClassSubjectTeacher-升级", notes = "insertUpgradeRelClassSubjectTeacher-升级")
    @GetMapping(value = "/insertUpgradeRelClassSubjectTeacher")
    public Result<?> insertUpgradeRelClassStudent(@RequestParam(name = "schoolYearId", required = true) String schoolYearId,
                                                  @RequestParam(name = "semesterId", required = true) String semesterId,
                                                  @RequestParam(name = "classId", required = false) String classId,
                                                  @RequestParam(name = "gradeId", required = false) String gradeId) {

        Assert.hasText(classId + gradeId, "班级或者年级Id没有传递，无法升年级");
        Assert.hasText(schoolYearId, "目标学年没有传递，请选择");
        Assert.hasText(semesterId, "目标学期没有传递，请选择");
        RelClassSubjectTeacherVO relClassSubjectTeacherVO = new RelClassSubjectTeacherVO();
        relClassSubjectTeacherVO.setSchoolYearId(schoolYearId);
        relClassSubjectTeacherVO.setSemesterId(semesterId);
        if (!StringUtil.isEmpty(classId)) {
            relClassSubjectTeacherVO.setClassId(classId);
        }
        if (!StringUtil.isEmpty(gradeId)) {
            relClassSubjectTeacherVO.setGradeId(gradeId);
        }
        if(relClassSubjectTeacherService.insertUpgradeRelClassSubjectTeacher(relClassSubjectTeacherVO) > 0){
            return Result.OK("班级科目教师升级成功！");
        }else {
            return Result.OK("班级科目教师升级失败！");
        }
    }


    /**
     * 导出excel
     *
     * @param request
     * @param relClassSubjectTeacher
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RelClassSubjectTeacher relClassSubjectTeacher) {
        return super.exportXls(request, relClassSubjectTeacher, RelClassSubjectTeacher.class, "rel_class_subject_teacher");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RelClassSubjectTeacher.class);
    }

}
